		include	Common.asm

; ==========================================================
DataSeg		segment para public 'data'

	GraphOriginX	word 0
	GraphOriginY	word 0
	GraphClipLeft	word 0
	GraphClipTop	word 0
	GraphClipRight	word 320
	GraphClipBottom	word 200

	GraphOldOriginX		word 0
	GraphOldOriginY		word 0
	GraphOldClipLeft	word 0
	GraphOldClipTop		word 0
	GraphOldClipRight	word 320
	GraphOldClipBottom	word 200

	ResourceFileName	db	"Resource.pcx",0

DataSeg		ends

; ==========================================================
CodeSeg		segment para public 'code'
	assume	ds: DataSeg



; ----------------------------------------------------------
; SetGraphicMode - Sets graphic mode
; ----------------------------------------------------------
; Input:
;	al	Graphic Mode
; Output:	none
; ----------------------------------------------------------
SetGraphicMode	proc

	; Set Video mode - Display Mode
	mov	ah, 0h		; ah = 0h - Set Video mode
	int	10h

	ret

SetGraphicMode	endp



; ----------------------------------------------------------
; LoadResource - Loads resource
; ----------------------------------------------------------
; Input:	none
; Output:	none
; ----------------------------------------------------------
LoadResource	proc

	mov	ax, ResourceBufferAddr
	mov	es, ax
	lea	bx, ResourceFileName
	call	LoadPCX

	ret

LoadResource	endp



; ----------------------------------------------------------
; DrawBorder - Draws border
; ----------------------------------------------------------
; Input:
;	[bp+2]	ret addr
;	[bp+4]	shadow color
;	[bp+6]	highlight color
;	[bp+8]	ySize
;	[bp+10]	xSize
;	[bp+12]	y
;	[bp+14]	x
; Output:	none
; ----------------------------------------------------------
DrawBorder	proc

	local	dummy: word

	push	ax

	push	[bp+14]
  	push	[bp+12]
	push	[bp+10]
	mov	ax, 0
	mov	ax, 1
	push	ax
	mov	ax, [bp+6]
	mov	ah, 0
	push	ax
	call	FillRect

	push	[bp+14]
  	push	[bp+12]
	mov	ax, 1
	push	ax
	push	[bp+8]
	mov	ax, [bp+6]
	push	ax
	call	FillRect

	push	[bp+14]
	mov	ax, [bp+12]
	add	ax, [bp+8]
	dec	ax
	push	ax
	push	[bp+10]
	mov	ax, 1
	push	ax
	mov	ax, [bp+4]
	push	ax
	call	FillRect

	mov	ax, [bp+14]
	add	ax, [bp+10]
	dec	ax
	push	ax
	mov	ax, [bp+12]
	push	ax
	mov	ax, 1
	push	ax
	push	[bp+8]
	mov	ax, [bp+4]
	push	ax
	call	FillRect

	pop	ax

	ret	12

DrawBorder	endp



; ----------------------------------------------------------
; FillRect - Draws filled rectangle
; ----------------------------------------------------------
; Input:
;	[bp+2]	ret addr
;	[bp+4]	color
;	[bp+6]	ySize
;	[bp+8]	xSize
;	[bp+10]	y
;	[bp+12]	x
; Output:	none
; ----------------------------------------------------------
FillRect	proc

	local left:word, top:word
	local right:word, bottom:word

	push	ax
	push	cx
	push	dx
	push	di
	push	es

	; calculate left/right coordination
	mov	ax, [bp+12]
	add	ax, GraphOriginX
	mov	left, ax
	add	ax, [bp+8]
	mov	right, ax

	; clip left coordination
	mov	ax, left
	cmp	ax, GraphClipLeft
	jnl	FillRectSkipClipLeft
	
	mov	ax, GraphClipLeft
	mov	left, ax

FillRectSkipClipLeft:
	; calculate top/bottom coordination
	mov	ax, [bp+10] ;y
	add	ax, GraphOriginY
	mov	top, ax
	add	ax, [bp+6]
	mov	bottom, ax

	; clip top coordination
	mov	ax, top
	cmp	ax, GraphClipTop
	jnl	FillRectSkipClipTop
	
	mov	ax, GraphClipTop
	mov	top, ax

FillRectSkipClipTop:
	mov	ax, 09000h
	mov	es, ax
	mov	dx, top

FillRectYLoop:
	cmp	dx, bottom
	jge	ExitYLoop
	cmp	dx, GraphClipBottom
	jge	ExitYLoop
	cmp	dx, GraphScreenHeight
	jge	ExitYLoop

	mov	ax, dx
	push	dx
	mov	dx, GraphScreenWidth
	imul	dx
	pop	dx
	mov	di, ax
	add	di, left

	mov	cx, left

FillRectXLoop:
	cmp	cx, right
	jge	ExitXLoop
	cmp	cx, GraphClipRight
	jge	ExitXLoop
	cmp	cx, GraphScreenWidth
	jge	ExitXLoop

	mov	al, [bp+4]
	mov	es:[di], al

	inc	di
	inc	cx

	jmp	FillRectXLoop

ExitXLoop:
	inc	dx

	jmp 	FillRectYLoop

ExitYLoop:
	pop	es
	pop	di
	pop	dx
	pop	cx
	pop	ax

	ret	10

FillRect	endp


; ----------------------------------------------------------
; ClearScreen - Clears screen
; ----------------------------------------------------------
; Input:	none
; Output:	none
; ----------------------------------------------------------
ClearScreen	proc

	mov	GraphOriginX, 0
	mov	GraphOriginY, 0

	mov	bx, 0
	push	bx
	push	bx
	mov	bx, GraphScreenWidth
	push	bx
	mov	bx, GraphScreenHeight
	push	bx
	mov	bx, 0
	push	bx
	call	FillRect
	ret

ClearScreen	endp


; ----------------------------------------------------------
; Present - Presents back buffer to screen
; ----------------------------------------------------------
; Input:	none
; Output:	none
; ----------------------------------------------------------
Present		proc

	push	ax
	push	bx
	push	dx
	push	cx
	push	si
	push	di
	push	ds
	push	es

	mov	ax, 09000h
	mov	ds, ax
	mov	ax, 0a000h
	mov	es, ax

	mov	ax, GraphScreenWidth
	shr	ax, 1
	mov	bx, GraphScreenHeight
	mul	bx
	mov	cx, ax

	mov	si, 0
	mov	di, 0
	rep	movsw
	
	pop	es
	pop	ds
	pop	di
	pop	si
	pop	dx
	pop	cx
	pop	bx
	pop	ax

	ret

Present		endp


CodeSeg		ends

		end

